package com.njuptB180316.boki.dao;

import com.njuptB180316.boki.dataobject.Book;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface BookMapper {
    @Delete({
        "delete from book",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into book (id, name, ",
        "num, picture, brief, ",
        "author)",
        "values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, ",
        "#{num,jdbcType=INTEGER}, #{picture,jdbcType=VARCHAR}, #{brief,jdbcType=VARCHAR}, ",
        "#{author,jdbcType=VARCHAR})"
    })
    int insert(Book record);

    @Select({
        "select",
        "id, name, num, picture, brief, author",
        "from book",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="num", property="num", jdbcType=JdbcType.INTEGER),
        @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
        @Result(column="brief", property="brief", jdbcType=JdbcType.VARCHAR),
        @Result(column="author", property="author", jdbcType=JdbcType.VARCHAR)
    })
    Book selectByPrimaryKey(Integer id);

    @Select({
        "select",
        "id, name, num, picture, brief, author",
        "from book"
    })
    @Results(id = "select", value = {
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="num", property="num", jdbcType=JdbcType.INTEGER),
        @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
        @Result(column="brief", property="brief", jdbcType=JdbcType.VARCHAR),
        @Result(column="author", property="author", jdbcType=JdbcType.VARCHAR)
    })
    List<Book> selectAll();

    @Update({
        "update book",
        "set name = #{name,jdbcType=VARCHAR},",
          "num = #{num,jdbcType=INTEGER},",
          "picture = #{picture,jdbcType=VARCHAR},",
          "brief = #{brief,jdbcType=VARCHAR},",
          "author = #{author,jdbcType=VARCHAR}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Book record);

    @Select({
            "select",
            "id, name, num, picture, brief, author",
            "from book",
            "limit #{offset}, #{size}"
    })
    @ResultMap("select")
    List<Book> selectByPagination(@Param("offset") int offset, @Param("size") int size);

    @Select({
            "select",
            "count(*)",
            "from book"
    })
    int countRecords();

    @Select({
            "select",
            "name, num, picture, brief, author",
            "from book",
            "where name like concat('%',#{search},'%')"
    })
    @ResultMap("select")
    List<Book> searchByName(@Param("search") String search);

    @Select({
            "select",
            "MAX(id)",
            "from book"
    })
    int selectMaxId();
}